#!/bin/sh

# 
# Author David Bruno Cortarello <Nomius>. Redistribute under the terms of the
# BSD-lite license. Bugs, suggests, nor projects: dcortarello@gmail.com
#
# Program: kpkg
# Version: 4.0a
#
#
# Copyright (c) 2002-2010, David B. Cortarello
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#  * Redistributions of source code must retain the above copyright notice
#    and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright notice
#    and the following disclaimer in the documentation and/or other materials
#    provided with the distribution.
#  * Neither the name of Kwort nor the names of its contributors may be used
#    to endorse or promote products derived from this software without
#    specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

usage() {
  cat << EOF

Usage: makepkg package_name.tar.xz

Makes a Kwort compatible "*.tar.xz" package containing the contents of the 
current and all subdirectories. You may add any of your own sh-compatible
shell scripts to this file and rebuild the package if you wish.

options:  -c, --chown     resets all permissions to root:root 755
          -z, --gzman     compress with gzip the manpages in the package
          -s, --strip     strip binaries and libraries in the package

If these options are not set, makepkg will assume none will be used

EOF
}

CHOWN=n
STRIP=n
GZMAN=n
PKG=""

for param in "${@}"; do
	if [ "${param}" = "--chown" -o "${param}" = "-c" ]; then
		CHOWN=y
	elif [ "${param}" = "--strip" -o "${param}" = "-s" ]; then
		STRIP=y
	elif [ "${param}" = "--gzman" -o "${param}" = "-z" ]; then
		GZMAN=y
	elif [ "${param}" = "--help" -o "${param}" = "-h" ]; then
		usage
		exit 0
	else
		if [ "`basename "${param}" .tar.xz`" = ${param} ]; then
			echo "Ooops, wrong package name"
			exit 2
		fi
		PKG=`basename ${param} .xz`
	fi
done

if [ "${PKG}" = "" ]; then
	echo "No package name given"
	exit 2
fi

# Change permissions and owner
if [ $CHOWN = "y" ]; then
	find . -type d | xargs chmod -v 755
	find . -type d | xargs chown -v root:root
fi

# Compress manpages with gzip
if [ $GZMAN = "y" -a -d usr/man ]; then
	# Compress manpages with gzip
	find usr/man -type f | xargs gzip
	for link in `find usr/man -type l`; do
		orig=`readlink ${link}`
		rm ${link}
		ln -sf `basename ${orig}`.gz ${link}.gz
	done
fi

# Strip binaries, shared libraries and static libraries
if [ "$STRIP" = "y" ]; then
	find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
	find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
	find . | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs strip --strip-debug 2> /dev/null
fi

# Make the package
tar cpf ${PKG} .
xz ${PKG}

# EOF

